package com.example.demo.order.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeOrderinfoSyncRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeOrderinfoSyncResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

/**
 * Created at 2021/3/5 1:39 下午
 *
 * @author Alex
 * description
 */
@Slf4j
@Configuration
public class Test {


    @Value("${alipay.gatewayUrl}")
    private static String serverUrl;

    @Value("${alipay.app_id}")
    private static String appId;

    @Value("${server.port}")
    private static String publicKey;

    @Value("${alipay.merchant_private_key}")
    private static String privateKey;


    public static void main(String args[]) throws AlipayApiException {
        System.out.println(privateKey);
        System.out.println(publicKey);
        query("2021030522001405931420526118");
    }

    public static void sync() throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2021001198694698", "your private_key", "json", "GBK", "alipay_public_key", "RSA2");
        AlipayTradeOrderinfoSyncRequest request = new AlipayTradeOrderinfoSyncRequest();
        request.setBizContent("{" +
                "\"trade_no\":\"2018061021001004680073956707\"," +
                "\"orig_request_no\":\"HZ01RF001\"," +
                "\"out_request_no\":\"HZ01RF001\"," +
                "\"biz_type\":\"CREDIT_AUTH\"," +
                "\"order_biz_info\":\"\\\"{\\\\\\\"status\\\\\\\":\\\\\\\"COMPLETE\\\\\\\"}\\\"\"" +
                "  }");
        AlipayTradeOrderinfoSyncResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }

    public static void query(String outId) throws AlipayApiException {
        DefaultAlipayClient client=  new DefaultAlipayClient(serverUrl, appId, privateKey, "json", "GBK", publicKey, "RSA2");
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(outId);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(model);
        AlipayTradeQueryResponse response = client.execute(request);
    }
}
